﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BUS.DAL;

namespace BUS
{
    public class DatHangBUS
    {
        #region PROPERTIES
        public int ID { get; set; }

        public string MaDatHang { get; set; }

        #region KHACHHANG
        public KhachHangBUS KhachHang { get; set; }

        public string TenKhacHang
        {
            get
            {
                return KhachHang == null ? string.Empty : KhachHang.HoTen;
            }
        }


        #endregion

        private decimal thanhTien;

        public decimal ThanhTien
        {
            get
            {
                return GetThanhTien();
            }
            set { thanhTien = value; }
        }

        private decimal GetThanhTien()
        {
            var total = chiTietDatHangs.Sum(ct => ct.ThanhTien);
            thanhTien = total;
            return thanhTien;
        }

        public string strThanhTien
        {
            get
            {
                if (chiTietDatHangs.Count > 0)
                    GetThanhTien();
                return String.Format("{0:0,0 triệu VNĐ}", thanhTien / 1000000);
            }
        }

        public DateTime ThoiGianDatHang { get; set; }

        public string strThoiGianDatHang
        {
            get
            {
                return String.Format("{0:dd/MM/yyyy - hh:mm:ss tt}", ThoiGianDatHang);
            }
        }

        private List<ChiTietDatHangBUS> chiTietDatHangs;
        public List<ChiTietDatHangBUS> ChiTietDatHangs
        {
            get { return chiTietDatHangs ?? (chiTietDatHangs = new List<ChiTietDatHangBUS>()); }
            set { chiTietDatHangs = value; }
        }

        public int TrangThai { get; set; }

        public int TongSoLuong
        {
            get
            {
                return chiTietDatHangs == null ? 0 : (int)chiTietDatHangs.Sum(p => p.SoLuong);
            }
        }

        #endregion


        public void Add(bool account)
        {
            if (!account)
                KhachHang.ID = KhachHangDAL.Insert(this.KhachHang);

            this.ID = DatHangDAL.Insert(this);

            ChiTietDatHangDAL.InsertAll(this);
        }

        public static List<DatHangBUS> GetListWaiting()
        {
            return DatHangDAL.GetListWaiting();
        }

        public void UpdateDuyet()
        {
            //this.thanhTien = GetThanhTien();
            this.TrangThai = DatHangDAL.UpdateDuyet(this);
        }

        public static List<DatHangBUS> GetListChecked()
        {
            return DatHangDAL.GetListChecked();
        }

        public void Delete()
        {

            //Xóa khách hàng đặt hàng
            KhachHangDAL.Delete(this);

            //Xóa phiếu đặt hàng
            DatHangDAL.Delete(this);
        }
        #region d
        public static List<DatHangBUS> GetAll()
        {
            return DatHangDAL.GetAll();
        }
        public string SoDTKH
        {
            get { return KhachHang.SoDienThoai; }
        }
        public string EmailKH
        {
            get { return KhachHang.Email; }
        }
        public string strTrangThai
        {
            get
            {
                string resuilt="";
                switch (TrangThai)
                {
                    case 1: resuilt = "Chưa xác nhận"; break;
                    case 2: resuilt = "Chưa giao"; break;
                    case 3: resuilt = "Đã giao"; break;
                }
                return resuilt;
            }
        }
        public static DatHangBUS GetByID(int ID)
        {
            return DatHangDAL.GetByID(ID);
        }
        public void UpdateTrangThai()
        {
            DatHangDAL.updateTrangThai(this);
        }
        public void Xoa()
        {
            DatHangDAL.Xoa(this);
        }
        #endregion


    }
}
